package com.pactera.asmp.server.restfullapi;

import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.logprint.utils.CollectionUtils;
import com.pactera.asmp.server.entity.ProjectDeviceBindDto;
import com.pactera.asmp.server.entity.ProjectDeviceDbcBindDto;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.DeviceProjectDbcVo;
import com.pactera.asmp.server.pojo.TProjectDeviceDbc;
import com.pactera.asmp.server.service.IProjectDeviceDbcService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfulProjectDeviceDbcApi
 * @Description: RestfulProjectDeviceDbcApi
 * @Date 2020/05/30 22:59
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class RestfulProjectDeviceDbcApi {
    @Autowired
    private IProjectDeviceDbcService projectDeviceDbcService;

    /**
     * @api {POST} /api/v1/proDevice/batchBindDbc batchBindDbc
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName batchBindDbc
     * @apiDescription 设备批量绑定 dbc 文件 (can 信号诊断->dbc配置)
     * @apiParam (请求体) {Number} projectId 项目ID
     * @apiParam (请求体) {Array} list 设备dbc集合
     * @apiParam (请求体) {Number} list.dbcId dbc文件表主键
     * @apiParam (请求体) {Number} list.deviceId 设备表主键
     * @apiParamExample 请求体示例
     * {"list":[{"deviceId":1591,"dbcId":1514}],"projectId":4126}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":7097,"data":{},"message":"8Jvvo"}
     */
    @PostMapping("/proDevice/batchBindDbc")
    public ResponseResult batchBindDbc(@RequestBody ProjectDeviceDbcBindDto projectDeviceDbcBindDto) {
        log.debug("/api/v1/proDevice/batchBindDbc projectDeviceDbcBindDto: {}", projectDeviceDbcBindDto);
        ResponseResult responseResult;
        if (null == projectDeviceDbcBindDto || CollectionUtils.isEmpty(projectDeviceDbcBindDto.getList())) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectDeviceDbcService.updateDbc(projectDeviceDbcBindDto));
        return responseResult;
    }

    /**
     * @api {POST} /api/v1/proDevice/batchUnBind projectDeviceBatchUnBind
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName projectDeviceBatchUnBind
     * @apiDescription 项目设备批量解绑
     * @apiParam (请求体) {Array} requestBody
     * @apiParam (请求体) {Number} requestBody.projectId 项目ID
     * @apiParam (请求体) {Number} requestBody.deviceId 设备表主键
     * @apiParamExample 请求体示例
     * [{"projectId":9824,"deviceId":6465}]
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":9393,"data":{},"message":"qGWG6TVS7"}
     */
    @PostMapping("/proDevice/batchUnBind")
    public ResponseResult projectDeviceBatchUnBind(@RequestBody List<TProjectDeviceDbc> projectDeviceDbcs) {
        log.debug("/api/v1/proDevice/batchUnBind projectDeviceDbcs: {}", projectDeviceDbcs);
        ResponseResult responseResult;
        if (null == projectDeviceDbcs ) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectDeviceDbcService.batchDelete(projectDeviceDbcs));
        return responseResult;
    }

    /**
     * @api {GET} /api/v1/proDevice/list projectDeviceList
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName projectDeviceList
     * @apiDescription 根据 projectId 或 deviceId 获取项目设备绑定关系
     * @apiParam (请求参数) {Number} projectId 项目ID，可选
     * @apiParam (请求参数) {Number} deviceId 设备ID，可选
     * @apiParamExample 请求参数示例
     * projectId=3769&deviceId=5557
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":4383,"data":{},"message":"LVxvEDG"}
     */
    @GetMapping("/proDevice/list")
    public ResponseResult projectDeviceList(Integer projectId, Integer deviceId) {
        log.debug("/api/v1/proDevice/list projectId: {}, deviceId: {}", projectId, deviceId);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectDeviceDbcService.selective(projectId, deviceId));
        return responseResult;
    }


    /**
     * @api {GET} /api/v1/proDevice/prodbc getProdbcList
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName projectDeviceList2
     * @apiDescription 根据设备ID查询可用的项目DBC文件
     * @apiParam (请求参数) {Number} projectId 项目ID
     * @apiParam (请求参数) {Number} deviceId 设备ID
     * @apiParam (请求参数) {String} duid 设备号
     * @apiParamExample 请求参数示例
     * projectId=2877&deviceId=1098
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} 请求头样例:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":1565,"data":{},"message":"lQus"}
     */
    @GetMapping("/proDevice/prodbc")
    public ResponseResult getProdbcList(Integer projectId, Integer deviceId, String duid) {
        log.debug("/api/v1/proDevice/prodbc projectId: {}, deviceId: {}, duid: {}", projectId, deviceId,duid);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectDeviceDbcService.selective2(projectId, deviceId,duid));
        return responseResult;
    }

    /**
     * @api {GET} /api/v1/proDevice/queryProjectAndDbcByDuid queryProjectAndDbcByDuid
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName queryProjectAndDbcByDuid
     * @apiDescription 根据 duid 获取对应已绑定项目名和dbc名 (simulator端需根据duid获取项目及dbc解析文件)
     * @apiParam (请求参数) {String} duid 设备duid
     * @apiParamExample 请求参数示例
     * duid=DsHp8
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":5461,"data":{},"message":"LCNFLzs"}
     */
    @GetMapping("/proDevice/queryProjectAndDbcByDuid")
    public ResponseResult queryProjectAndDbcByDuid(String duid) {
        log.debug("/api/v1/proDevice/queryProjectAndDbcByDuid duid: {}", duid);
        if (StringUtils.isBlank(duid)) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectDeviceDbcService.queryProjectAndDbcByDuid(duid));
        return responseResult;
    }

    /**
     * @api {POST} /api/v1/proDevice/batchBind batchBind
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName batchBind
     * @apiDescription 批量绑定项目设备
     * @apiParam (请求体) {Number} projectId 项目ID
     * @apiParam (请求体) {Array} deviceIds 设备ID集合（全量）
     * @apiParamExample 请求体示例
     * {"deviceIds":[2779],"projectId":6354}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":5749,"data":{},"message":"n6B"}
     */
    @PostMapping("/proDevice/batchBind")
    public ResponseResult batchBind(@RequestBody ProjectDeviceBindDto projectDeviceBindDto) {
        log.debug("/api/v1/proDevice/batchBind projectDeviceBindDto: {}", projectDeviceBindDto);
        ResponseResult responseResult;
        if (null == projectDeviceBindDto || null == projectDeviceBindDto.getProjectId()) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectDeviceDbcService.batchBind(projectDeviceBindDto));
        return responseResult;
    }


    /**
     * @api {POST} /api/v1/proDevice/deviceBind deviceBind
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName deviceBind
     * @apiDescription 修改设备信息
     * @apiParam (请求体) {Number} deviceId 设备ID
     * @apiParam (请求体) {String} nickName 设备昵称
     * @apiParam (请求体) {Array} projectDbcInfos 绑定的项目DBC文件列表（全量数据）
     * @apiParam (请求体) {Number} projectDbcInfos.projectId 项目ID
     * @apiParam (请求体) {Number} projectDbcInfos.dbcId dbc文件ID
     * @apiParamExample 请求体示例
     * {"nickName":"JFs7tREfoi","projectDbcInfos":[{"projectId":5370,"dbcId":7792}],"deviceId":547}
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} 请求头样例:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":8930,"data":{},"message":"RJcBbd"}
     */
    @PostMapping("/proDevice/deviceBind")
    public ResponseResult deviceBind(@RequestBody DeviceProjectDbcVo deviceProjectDbcVo) {
        log.info("/api/v1/proDevice/deviceBind");
        return projectDeviceDbcService.deviceBind(deviceProjectDbcVo);
    }


    /**
     * @api {GET} /api/v1/proDevice/dbcsByDeviceId getProjectDeviceDbcsByDeviceId
     * @apiVersion 1.0.0
     * @apiGroup RestfulProjectDeviceDbcApi
     * @apiName getProjectDeviceDbcsByDeviceId
     * @apiDescription 根据设备ID获取所有项目绑定的DBC文件
     * @apiParam (请求参数) {Number} deviceId 设备ID
     * @apiParamExample 请求参数示例
     * deviceId=5759
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} 请求头样例:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":2948,"data":{},"message":"Uh0jT"}
     */
    @GetMapping("/proDevice/dbcsByDeviceId")
    public ResponseResult getProjectDeviceDbcsByDeyyviceId(@RequestParam("deviceId") Integer deviceId) {
        log.debug("/api/v1/proDevice/dbcsByDeviceId : {}", deviceId);
        return ResponseResult.defaultSuccess(projectDeviceDbcService.getProjectDeviceDbcsByDeviceId(deviceId));
    }
}
